package icedot.library.common.adapter;

import java.util.List;

import android.app.ActivityManager;
import android.content.Context;
import android.database.DataSetObserver;
import android.text.format.Formatter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import icedot.library.common.base.BaseActivity;
import icedot.library.common.base.Logger;

public class ListView_Adapter<T> extends BaseAdapter
{
    private List<T>        _list;
    private Context        _context;
    private Class<?>       _viewItemClass;
    private BaseActivity   _activity;
    private String         _uuid;
    private boolean        _pause = false;
    private Object         _flag;

    public ListView_Adapter(BaseActivity activity, List<T> list,Class<?> viewClass)
    {
        this._context = activity.getBaseContext();
        _uuid = activity.getUuid();
        this._list = list;
        this._activity = activity;
        this._viewItemClass = viewClass;
    }

    public ListView_Adapter(String uuid,BaseActivity activity, List<T> list,Class<?> viewClass)
    {
        this._context = activity.getBaseContext();
        this._list = list;
        this._activity = activity;
        _uuid = uuid;
        this._viewItemClass = viewClass;
    }

    @Override
    public int getCount()
    {
        if (_list == null)
        {
            return 0;
        }
        return _list.size();
    }

    @Override
    public Object getItem(int position)
    {
        if (_list == null)
        {
            return null;
        }
        return _list.size();
    }

    @Override
    public long getItemId(int position)
    {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        if (_list == null || _list.size() == 0)
            return null;
        ListView_AdapterItem viewItem = null;

        ActivityManager            am = (ActivityManager) _activity.getSystemService(Context.ACTIVITY_SERVICE);
        ActivityManager.MemoryInfo mi = new ActivityManager.MemoryInfo();
        am.getMemoryInfo(mi);
//        Logger.showDebugMsg("内存使用量:" + am.getMemoryClass());
//        Logger.showDebugMsg("position=" + position);
        if(convertView == null)
        {
            try
            {
                viewItem = (ListView_AdapterItem)_viewItemClass.newInstance(); 
                if(viewItem == null)
                {
                    return null;
                }
            }
            catch(Exception e)
            {
                return null;
            }
            convertView = viewItem.initView(_context,parent);
            if(this._activity != null)
            {
                viewItem.setBaseActivity(this._activity);
            }
            convertView.setTag(viewItem);
        }
        else
        {
            viewItem = (ListView_AdapterItem)convertView.getTag();
            if(viewItem==null)
                return null;
        }

        ShowObject obj = new ShowObject();
        obj._obj = _list.get(position);
        obj._uuid = _uuid;
        obj._pause = _pause;
        obj._flag = _flag;
        viewItem.updateView(_list,obj);

        return convertView;
    }

    @Override
    public void registerDataSetObserver(DataSetObserver observer)
    {
        if(observer != null)
        {
            super.registerDataSetObserver(observer);
        }
    }

    @Override
    public void unregisterDataSetObserver(DataSetObserver observer)
    {
        if(observer !=null)
        {
            super.unregisterDataSetObserver(observer);
        }
    }

    public void onResume()
    {
//        _pause = true;
        _pause = false;
        if(_list.size() > 0)
        {
            notifyDataSetChanged();
        }
    }

    public void onPause()
    {
        _pause = true;
        if(_list.size() > 0)
        {
            notifyDataSetChanged();
        }
    }

    public void setFlag(Object flag)
    {
        _flag = flag;
    }

    public List<T> getList()
    {
        return _list;
    }

    public void setList(List<T> list) 
    {
        this._list = list;
    }
}
